﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;

namespace Tailord
{
    public partial class Fiyat : XForm
    {
        public Fiyat()
        {
            InitializeComponent();
        }

        private void Fiyat_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;

            GetDetails("Firma;Bayi;User;Customer;Kumas;Kalite;Order", "GetData");

            gv.Bands.Clear();
            gv.Images = bgvIcons;

            int idx = 0;
            foreach (string str in "Grup;Toptan;Perakende".Split(";".ToCharArray()))
            {
                bands.Add(gv.Bands.AddBand(str));
                bands[idx].ImageIndex = idx++;
            }

            foreach (string colName in "KaliteId,Name,Uretici".Split(','))
                bands[0].Columns.Add(gv.Columns[colName]);


            gv.Init("GetPerakende");
            foreach (DataRow dr in gv.Dataset().Tables[1].Rows)
            {
                int FirmaId = (int)dr["FirmaId"];

                gv.SetCaptions(FirmaId.ToString() + ":" +
                    dr["Firma"].ToString() + "\r\n" +
                    dr["Dvz"].ToString());
                gv.FormatAsNumber(2, FirmaId.ToString());

                bands[FirmaId > 1000 ? 1 : 2].Columns.Add(gv.Columns[FirmaId.ToString()]);
            }

            gv.Bands[0].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;


            gv.Columns["Name"].ColumnEdit = riKalite;
            gv.MakeEditable("KaliteId", "Name", "Uretici");

            gv.ColumnPanelRowHeight = 36;
            gv.Appearance.HeaderPanel.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            gv.Appearance.HeaderPanel.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;

            gv.RowCellStyle += gv_RowCellStyle;

            foreach (GridColumn col in gv.Columns)
                if (col.FieldName.Contains("Tarih"))
                    gv.FormatAsDate(col.FieldName);

            gv.BestFitColumns();

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
        }


        List<GridBand> bands = new List<GridBand>();
        

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            switch (e.Column.FieldName)
            {
                case "5":
                case "Name":
                case "KumasKodu":
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                    break;
            }
        }

        public RepositoryItemGridLookUpEdit riKalite
        {
            get
            {
                return GetCustomGLE(ds.Kalite, "Name", "KaliteId");
            }
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.CloseEditor();
            gv.UpdateCurrentRow();

            string str = gv.GetChangesAsXml("Per", true);
            ExecQuery("UpdPerakende", gv.GetChangesAsXml("Per", true));

            Notify("Saved successfully.");
        }
    }
}
